﻿Shader "URP/Polar Shadow"
{
    Properties
    {
        _ShadowY ("ShadowY", float) = 0.001
    }
    SubShader
    {
        Pass
        {
            Name "MyShadow"

            Tags {
                "RenderType" = "Transparent"
                "IgnoreProjector" = "True"
                "UniversalMaterialType" = "Unlit"
                "RenderPipeline" = "UniversalPipeline"
            }
            
            Stencil
            {
                Ref 0
                Comp equal
                Pass incrWrap
                Fail keep
                ZFail keep
            }

            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            Cull back
            Offset -1, -1

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment fragment
            // GPU Instancing
            #pragma multi_compile_instancing

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            CBUFFER_START(UnityPerMaterial)
            float _ShadowY;
            CBUFFER_END

            struct Attributes
            {
                float4 positionOS   : POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct Varyings
            {
                float4 positionCS   : SV_POSITION;

                UNITY_VERTEX_INPUT_INSTANCE_ID
                UNITY_VERTEX_OUTPUT_STEREO
            };

            
            Varyings vert(Attributes input)
            {
                Varyings output = (Varyings)0;
                UNITY_SETUP_INSTANCE_ID(input);
                UNITY_TRANSFER_INSTANCE_ID(input, output);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

                float3 W = TransformObjectToWorld(input.positionOS.xyz);


                W /= input.positionOS.w;
                float3 lightDir = _MainLightPosition.xyz - W * _MainLightPosition.w;
                float shadowY = min(W.y , _ShadowY);
                float3 shadowPos = float3(0,shadowY,0);
                shadowPos.xz = W.xz - lightDir.xz * W.y / lightDir.y;


                //地图弯曲
                float3 V = TransformWorldToView(shadowPos);
                float delta = length(V) / 90;
                float curve = pow(delta, 6) * 100;
                V.y -= curve;
                W = TransformViewToWorld(V);

                output.positionCS = TransformWorldToHClip(W);
                return output;
            }
            
            
            half4 fragment(Varyings input): SV_TARGET
            {
                UNITY_SETUP_INSTANCE_ID(input);
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);

                return half4(0,0,0,0.5);
            }     
            ENDHLSL

        }
    }
}

